package com.mes.basicdata.service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.mes.basicdata.dao.CalendarDataDao;
import com.mes.basicdata.dao.iface.ICalendarDao;
import com.mes.basicdata.dao.iface.ICalendarDataDao;
import com.mes.basicdata.domain.Calendar;
import com.mes.basicdata.domain.CalendarData;
import com.mes.common.service.BasicService;

@Service
public class CalendarService extends BasicService<Calendar> {

	@Autowired
	private ICalendarDao calendarDao;
	
	@Autowired
	private ICalendarDataDao calendarDataDao;
	
	/**
	 * 获得基准日历
	 * @throws Exception 
	 */
	@DataProvider
	public List<Calendar> getBaseCalendar(Map<String, Object> params) throws Exception{
		return calendarDao.getBaseCalendar(params);
	}
	
	/**
	 * 日历保存
	 * @param dataItems
	 * @throws Exception
	 */
	@DataResolver
	public void saveCalendar(Map<String, Collection<Calendar>> dataItems) throws Exception {
		for (String key : dataItems.keySet()){
			Collection<Calendar> calendars = dataItems.get(key);
			//保存主表
			super.saveAll(calendars);
			
			//保存从表
			for (Calendar calendar : calendars){
				
				Set<CalendarData> calendarDatas = calendar.getCalendarDatas();
				
				if (calendarDatas != null){
					calendarDataDao.saveCalendarData(calendarDatas);
				}
			}
		}
		
	}
	
	@DataProvider
	public Collection<Calendar> getCalendarByDept(String deptUid) throws Exception{
		return calendarDao.findByProperty("deptUid", deptUid);
	}
	
}
